{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np # 数据处理最重要的模块\n",
    "import pandas as pd # 数据处理最重要的模块\n",
    "import scipy.stats as stats # 统计模块\n",
    "from datetime import datetime # 时间模块\n",
    "import scipy\n",
    "# import pymysql  # 导入数据库模块\n",
    "import statsmodels.api as sm"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "导入数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "rawdata1 = pd.read_excel('Lecture 2 p2p lending platforms.xlsx')\n",
    "rawdata2 = pd.read_excel('Lecture 2 Renrendai loans.xlsx')\n",
    "data1 = rawdata1[['OnlineTime_YMD','Bankrupt_WDZJ','Collapse', 'Benign','Fraud','RegCapital','Background',\n",
    "'Capitaldeposit','Obtaininvest','Joinasso','Autobid','Transright','Riskdeposit','Thirdguarantee']]\n",
    "data2 = rawdata2[['DEFAULT', 'INTEREST','BIDS','AMOUNT','CREDIT','HOUSE','CAR','HOUSE_L','CAR_L',\n",
    "                  'EDUCATION','WORKTIME','INCOME','AGE']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "OnlineTime_YMD      0\n",
       "Bankrupt_WDZJ     218\n",
       "Collapse            0\n",
       "Benign            218\n",
       "Fraud             218\n",
       "RegCapital          0\n",
       "Background          0\n",
       "Capitaldeposit      0\n",
       "Obtaininvest       32\n",
       "Joinasso           32\n",
       "Autobid             0\n",
       "Transright          0\n",
       "Riskdeposit        32\n",
       "Thirdguarantee     32\n",
       "dtype: int64"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#检查是否有缺失值\n",
    "missing1 = data1.isnull().sum()\n",
    "missing1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DEFAULT      0\n",
       "INTEREST     0\n",
       "BIDS         0\n",
       "AMOUNT       0\n",
       "CREDIT       0\n",
       "HOUSE        0\n",
       "CAR          0\n",
       "HOUSE_L      0\n",
       "CAR_L        0\n",
       "EDUCATION    4\n",
       "WORKTIME     6\n",
       "INCOME       2\n",
       "AGE          0\n",
       "dtype: int64"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "missing2 = data2.isnull().sum()\n",
    "missing2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "OnlineTime_YMD      int64\n",
       "Bankrupt_WDZJ     float64\n",
       "Collapse            int64\n",
       "Benign            float64\n",
       "Fraud             float64\n",
       "RegCapital        float64\n",
       "Background         object\n",
       "Capitaldeposit      int64\n",
       "Obtaininvest      float64\n",
       "Joinasso          float64\n",
       "Autobid             int64\n",
       "Transright          int64\n",
       "Riskdeposit       float64\n",
       "Thirdguarantee    float64\n",
       "dtype: object"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data1.dtypes"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "表1数据有1000条，考虑到未破产的清算时间数据确实，且和欺诈、良性且缺失值相等，因此后续需要对破产时间缺失值填充；对于其他缺失值，先看看对样本数量产生多大影响，如果影响不大就删除缺失值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "968"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "reg_data1=data1.dropna(subset=['Obtaininvest','Joinasso','Riskdeposit','Thirdguarantee'])\n",
    "num1_rows = reg_data1.shape[0]\n",
    "num1_rows"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "可见，共有968条数据，缺失值影响不大。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10000"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "num2_rows = data2.shape[0]\n",
    "num2_rows"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "表2有一万条数据，且缺失值较少，直接删去缺失值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "reg_data2=data2.dropna(subset=['EDUCATION','WORKTIME','INCOME'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "问题一：描述性统计"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>count</th>\n",
       "      <th>mean</th>\n",
       "      <th>std</th>\n",
       "      <th>min</th>\n",
       "      <th>25%</th>\n",
       "      <th>50%</th>\n",
       "      <th>75%</th>\n",
       "      <th>max</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Collapse</th>\n",
       "      <td>1000.0</td>\n",
       "      <td>0.782000</td>\n",
       "      <td>0.413094</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Benign</th>\n",
       "      <td>782.0</td>\n",
       "      <td>0.098465</td>\n",
       "      <td>0.298134</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Fraud</th>\n",
       "      <td>782.0</td>\n",
       "      <td>0.246803</td>\n",
       "      <td>0.431427</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>RegCapital</th>\n",
       "      <td>1000.0</td>\n",
       "      <td>596.064330</td>\n",
       "      <td>2328.221711</td>\n",
       "      <td>2.0</td>\n",
       "      <td>100.0</td>\n",
       "      <td>300.0</td>\n",
       "      <td>500.0</td>\n",
       "      <td>50000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Capitaldeposit</th>\n",
       "      <td>1000.0</td>\n",
       "      <td>0.191000</td>\n",
       "      <td>0.393286</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                 count        mean          std  min    25%    50%    75%  \\\n",
       "Collapse        1000.0    0.782000     0.413094  0.0    1.0    1.0    1.0   \n",
       "Benign           782.0    0.098465     0.298134  0.0    0.0    0.0    0.0   \n",
       "Fraud            782.0    0.246803     0.431427  0.0    0.0    0.0    0.0   \n",
       "RegCapital      1000.0  596.064330  2328.221711  2.0  100.0  300.0  500.0   \n",
       "Capitaldeposit  1000.0    0.191000     0.393286  0.0    0.0    0.0    0.0   \n",
       "\n",
       "                    max  \n",
       "Collapse            1.0  \n",
       "Benign              1.0  \n",
       "Fraud               1.0  \n",
       "RegCapital      50000.0  \n",
       "Capitaldeposit      1.0  "
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "descriptive_stats1 = data1[['Collapse', 'Benign','Fraud','RegCapital','Background','Capitaldeposit',\n",
    "'Obtaininvest','Joinasso','Autobid','Transright','Riskdeposit','Thirdguarantee']].describe()\n",
    "descriptive_stats1.T.to_excel('outcome2.1.1.xlsx', index=True)\n",
    "descriptive_stats1.T.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>count</th>\n",
       "      <th>mean</th>\n",
       "      <th>std</th>\n",
       "      <th>min</th>\n",
       "      <th>25%</th>\n",
       "      <th>50%</th>\n",
       "      <th>75%</th>\n",
       "      <th>max</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>DEFAULT</th>\n",
       "      <td>10000.0</td>\n",
       "      <td>0.1513</td>\n",
       "      <td>0.358359</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>INTEREST</th>\n",
       "      <td>10000.0</td>\n",
       "      <td>12.6219</td>\n",
       "      <td>2.273689</td>\n",
       "      <td>5.0</td>\n",
       "      <td>11.0</td>\n",
       "      <td>12.0</td>\n",
       "      <td>13.0</td>\n",
       "      <td>24.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>BIDS</th>\n",
       "      <td>10000.0</td>\n",
       "      <td>24.1506</td>\n",
       "      <td>41.342608</td>\n",
       "      <td>1.0</td>\n",
       "      <td>9.0</td>\n",
       "      <td>15.0</td>\n",
       "      <td>24.0</td>\n",
       "      <td>592.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>AMOUNT</th>\n",
       "      <td>10000.0</td>\n",
       "      <td>24545.8350</td>\n",
       "      <td>38280.756524</td>\n",
       "      <td>3000.0</td>\n",
       "      <td>8000.0</td>\n",
       "      <td>14400.0</td>\n",
       "      <td>26000.0</td>\n",
       "      <td>500000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>CREDIT</th>\n",
       "      <td>10000.0</td>\n",
       "      <td>2.1463</td>\n",
       "      <td>1.530990</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>7.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            count        mean           std     min     25%      50%      75%  \\\n",
       "DEFAULT   10000.0      0.1513      0.358359     0.0     0.0      0.0      0.0   \n",
       "INTEREST  10000.0     12.6219      2.273689     5.0    11.0     12.0     13.0   \n",
       "BIDS      10000.0     24.1506     41.342608     1.0     9.0     15.0     24.0   \n",
       "AMOUNT    10000.0  24545.8350  38280.756524  3000.0  8000.0  14400.0  26000.0   \n",
       "CREDIT    10000.0      2.1463      1.530990     1.0     1.0      2.0      3.0   \n",
       "\n",
       "               max  \n",
       "DEFAULT        1.0  \n",
       "INTEREST      24.4  \n",
       "BIDS         592.0  \n",
       "AMOUNT    500000.0  \n",
       "CREDIT         7.0  "
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "descriptive_stats2 = data2[['DEFAULT', 'INTEREST','BIDS','AMOUNT','CREDIT','HOUSE',\n",
    "'CAR','HOUSE_L','CAR_L','EDUCATION','WORKTIME','INCOME','AGE']].describe()\n",
    "descriptive_stats2.T.to_excel('outcome2.1.2.xlsx', index=True)\n",
    "descriptive_stats2.T.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Background\n",
       "民营系     92.4\n",
       "国企背景     4.6\n",
       "上市公司     1.6\n",
       "风投系      1.4\n",
       "Name: count, dtype: float64"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bg_counts = data1['Background'].value_counts()\n",
    "total_count = len(data1['Background'])\n",
    "marriage_percentages = bg_counts / total_count * 100\n",
    "marriage_percentages"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "问题二："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Optimization terminated successfully.\n",
      "         Current function value: 0.330132\n",
      "         Iterations 9\n",
      "                           Logit Regression Results                           \n",
      "==============================================================================\n",
      "Dep. Variable:                DEFAULT   No. Observations:                 9990\n",
      "Model:                          Logit   Df Residuals:                     9980\n",
      "Method:                           MLE   Df Model:                            9\n",
      "Date:                Tue, 31 Dec 2024   Pseudo R-squ.:                  0.2236\n",
      "Time:                        14:31:28   Log-Likelihood:                -3298.0\n",
      "converged:                       True   LL-Null:                       -4247.9\n",
      "Covariance Type:            nonrobust   LLR p-value:                     0.000\n",
      "==============================================================================\n",
      "                 coef    std err          z      P>|z|      [0.025      0.975]\n",
      "------------------------------------------------------------------------------\n",
      "const          0.5155      0.212      2.427      0.015       0.099       0.932\n",
      "CREDIT        -1.8927      0.082    -23.044      0.000      -2.054      -1.732\n",
      "HOUSE          0.1438      0.073      1.968      0.049       0.001       0.287\n",
      "CAR           -0.4586      0.080     -5.708      0.000      -0.616      -0.301\n",
      "HOUSE_L       -0.3307      0.091     -3.633      0.000      -0.509      -0.152\n",
      "CAR_L          0.1620      0.134      1.207      0.228      -0.101       0.425\n",
      "EDUCATION     -0.4156      0.040    -10.426      0.000      -0.494      -0.337\n",
      "WORKTIME       0.0090      0.034      0.264      0.792      -0.058       0.076\n",
      "INCOME         0.1160      0.025      4.592      0.000       0.066       0.165\n",
      "AGE            0.0254      0.005      4.936      0.000       0.015       0.036\n",
      "==============================================================================\n"
     ]
    }
   ],
   "source": [
    "X1 = reg_data2[['CREDIT','HOUSE','CAR','HOUSE_L','CAR_L','EDUCATION','WORKTIME','INCOME','AGE']]\n",
    "y1 = reg_data2['DEFAULT'] \n",
    "X1 = sm.add_constant(X1) \n",
    "logit_model = sm.Logit(y1, X1)\n",
    "result = logit_model.fit()\n",
    "print(result.summary())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "进行多重共线性检验"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     feature        VIF\n",
      "0      const  43.605240\n",
      "1     CREDIT   1.101509\n",
      "2      HOUSE   1.497020\n",
      "3        CAR   1.426349\n",
      "4    HOUSE_L   1.326349\n",
      "5      CAR_L   1.177347\n",
      "6  EDUCATION   1.072635\n",
      "7   WORKTIME   1.267157\n",
      "8     INCOME   1.203443\n",
      "9        AGE   1.396831\n"
     ]
    }
   ],
   "source": [
    "from statsmodels.stats.outliers_influence import variance_inflation_factor\n",
    "vif_data = pd.DataFrame()\n",
    "vif_data['feature'] = X1.columns\n",
    "vif_data['VIF'] = [variance_inflation_factor(X1.values, i) for i in range(X1.shape[1])]\n",
    "print(vif_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                            OLS Regression Results                            \n",
      "==============================================================================\n",
      "Dep. Variable:                   BIDS   R-squared:                       0.173\n",
      "Model:                            OLS   Adj. R-squared:                  0.172\n",
      "Method:                 Least Squares   F-statistic:                     232.1\n",
      "Date:                Tue, 31 Dec 2024   Prob (F-statistic):               0.00\n",
      "Time:                        14:31:29   Log-Likelihood:                -50383.\n",
      "No. Observations:                9990   AIC:                         1.008e+05\n",
      "Df Residuals:                    9980   BIC:                         1.009e+05\n",
      "Df Model:                           9                                         \n",
      "Covariance Type:            nonrobust                                         \n",
      "==============================================================================\n",
      "                 coef    std err          t      P>|t|      [0.025      0.975]\n",
      "------------------------------------------------------------------------------\n",
      "const        -50.8110      2.479    -20.497      0.000     -55.670     -45.952\n",
      "CREDIT         1.8652      0.257      7.248      0.000       1.361       2.370\n",
      "HOUSE          1.6099      0.926      1.738      0.082      -0.206       3.426\n",
      "CAR            4.2582      0.918      4.637      0.000       2.458       6.059\n",
      "HOUSE_L       -7.1289      1.030     -6.924      0.000      -9.147      -5.111\n",
      "CAR_L         -7.1951      1.482     -4.854      0.000     -10.101      -4.290\n",
      "EDUCATION     -2.0042      0.475     -4.218      0.000      -2.936      -1.073\n",
      "WORKTIME       2.4355      0.426      5.721      0.000       1.601       3.270\n",
      "INCOME         9.2260      0.308     29.918      0.000       8.622       9.831\n",
      "AGE            0.8126      0.066     12.235      0.000       0.682       0.943\n",
      "==============================================================================\n",
      "Omnibus:                    11602.380   Durbin-Watson:                   1.743\n",
      "Prob(Omnibus):                  0.000   Jarque-Bera (JB):          1282780.294\n",
      "Skew:                           6.139   Prob(JB):                         0.00\n",
      "Kurtosis:                      57.139   Cond. No.                         239.\n",
      "==============================================================================\n",
      "\n",
      "Notes:\n",
      "[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.\n"
     ]
    }
   ],
   "source": [
    "X2 = reg_data2[['CREDIT','HOUSE','CAR','HOUSE_L','CAR_L','EDUCATION','WORKTIME','INCOME','AGE']]\n",
    "y2 = reg_data2['BIDS'] \n",
    "X2 = sm.add_constant(X2) \n",
    "logit_model = sm.OLS(y2, X2)\n",
    "result = logit_model.fit()\n",
    "print(result.summary())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     feature        VIF\n",
      "0      const  43.605240\n",
      "1     CREDIT   1.101509\n",
      "2      HOUSE   1.497020\n",
      "3        CAR   1.426349\n",
      "4    HOUSE_L   1.326349\n",
      "5      CAR_L   1.177347\n",
      "6  EDUCATION   1.072635\n",
      "7   WORKTIME   1.267157\n",
      "8     INCOME   1.203443\n",
      "9        AGE   1.396831\n"
     ]
    }
   ],
   "source": [
    "vif_data = pd.DataFrame()\n",
    "vif_data['feature'] = X2.columns\n",
    "vif_data['VIF'] = [variance_inflation_factor(X2.values, i) for i in range(X2.shape[1])]\n",
    "print(vif_data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "问题4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/var/folders/_p/rfbd33cd5jggpfbv91zgdptr0000gn/T/ipykernel_25035/1391227758.py:2: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  reg_data1['Background']= reg_data1['Background'].apply(lambda x: 1 if x == '民营系' else 0)\n"
     ]
    }
   ],
   "source": [
    "#对产权性质赋值1-0 考虑到民营数量的绝对优势，只区分是否民营\n",
    "reg_data1['Background']= reg_data1['Background'].apply(lambda x: 1 if x == '民营系' else 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/var/folders/_p/rfbd33cd5jggpfbv91zgdptr0000gn/T/ipykernel_25035/2823859724.py:3: FutureWarning: A value is trying to be set on a copy of a DataFrame or Series through chained assignment using an inplace method.\n",
      "The behavior will change in pandas 3.0. This inplace method will never work because the intermediate object on which we are setting values always behaves as a copy.\n",
      "\n",
      "For example, when doing 'df[col].method(value, inplace=True)', try using 'df.method({col: value}, inplace=True)' or df[col] = df[col].method(value) instead, to perform the operation inplace on the original object.\n",
      "\n",
      "\n",
      "  reg_data1['Bankrupt_WDZJ'].fillna(max_value, inplace=True)\n",
      "/var/folders/_p/rfbd33cd5jggpfbv91zgdptr0000gn/T/ipykernel_25035/2823859724.py:3: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  reg_data1['Bankrupt_WDZJ'].fillna(max_value, inplace=True)\n",
      "/var/folders/_p/rfbd33cd5jggpfbv91zgdptr0000gn/T/ipykernel_25035/2823859724.py:5: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  reg_data1['Bankrupt_WDZJ'] = reg_data1['Bankrupt_WDZJ'].astype(int)\n"
     ]
    }
   ],
   "source": [
    "#填充方法：选取破产事件最晚的填充\n",
    "max_value = reg_data1['Bankrupt_WDZJ'].dropna().max()\n",
    "reg_data1['Bankrupt_WDZJ'].fillna(max_value, inplace=True)\n",
    "#由于时间是20240101这样表示的，计算存续天数不好计算，打算用python的日期函数，因此要转换日期格式\n",
    "reg_data1['Bankrupt_WDZJ'] = reg_data1['Bankrupt_WDZJ'].astype(int)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/var/folders/_p/rfbd33cd5jggpfbv91zgdptr0000gn/T/ipykernel_25035/70181702.py:5: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  reg_data1['Bankrupt_WDZJ'] = reg_data1['Bankrupt_WDZJ'].apply(convert_int_to_datetime)\n",
      "/var/folders/_p/rfbd33cd5jggpfbv91zgdptr0000gn/T/ipykernel_25035/70181702.py:6: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  reg_data1['OnlineTime_YMD'] = reg_data1['OnlineTime_YMD'].apply(convert_int_to_datetime)\n",
      "/var/folders/_p/rfbd33cd5jggpfbv91zgdptr0000gn/T/ipykernel_25035/70181702.py:8: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  reg_data1['Duration']=(reg_data1['Bankrupt_WDZJ']-reg_data1['OnlineTime_YMD']).dt.days\n"
     ]
    }
   ],
   "source": [
    "#定义日期转换函数\n",
    "def convert_int_to_datetime(int_date):\n",
    "    str_date = str(int_date)\n",
    "    return datetime.strptime(str_date, '%Y%m%d')\n",
    "reg_data1['Bankrupt_WDZJ'] = reg_data1['Bankrupt_WDZJ'].apply(convert_int_to_datetime)\n",
    "reg_data1['OnlineTime_YMD'] = reg_data1['OnlineTime_YMD'].apply(convert_int_to_datetime)\n",
    "#计算存续期\n",
    "reg_data1['Duration']=(reg_data1['Bankrupt_WDZJ']-reg_data1['OnlineTime_YMD']).dt.days\n",
    "reg_data=reg_data1[['Duration','Collapse','RegCapital','Background','Capitaldeposit','Obtaininvest',\n",
    "                    'Joinasso','Autobid','Transright','Riskdeposit','Thirdguarantee']]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>model</th>\n",
       "      <td>lifelines.CoxPHFitter</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>duration col</th>\n",
       "      <td>'Duration'</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>event col</th>\n",
       "      <td>'Collapse'</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>baseline estimation</th>\n",
       "      <td>breslow</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>number of observations</th>\n",
       "      <td>968</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>number of events observed</th>\n",
       "      <td>774</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>partial log-likelihood</th>\n",
       "      <td>-4650.83</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>time fit was run</th>\n",
       "      <td>2024-12-31 06:31:29 UTC</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div><table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th style=\"min-width: 12px;\"></th>\n",
       "      <th style=\"min-width: 12px;\">coef</th>\n",
       "      <th style=\"min-width: 12px;\">exp(coef)</th>\n",
       "      <th style=\"min-width: 12px;\">se(coef)</th>\n",
       "      <th style=\"min-width: 12px;\">coef lower 95%</th>\n",
       "      <th style=\"min-width: 12px;\">coef upper 95%</th>\n",
       "      <th style=\"min-width: 12px;\">exp(coef) lower 95%</th>\n",
       "      <th style=\"min-width: 12px;\">exp(coef) upper 95%</th>\n",
       "      <th style=\"min-width: 12px;\">cmp to</th>\n",
       "      <th style=\"min-width: 12px;\">z</th>\n",
       "      <th style=\"min-width: 12px;\">p</th>\n",
       "      <th style=\"min-width: 12px;\">-log2(p)</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>RegCapital</th>\n",
       "      <td>0.00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>-0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.30</td>\n",
       "      <td>0.77</td>\n",
       "      <td>0.38</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Background</th>\n",
       "      <td>0.55</td>\n",
       "      <td>1.73</td>\n",
       "      <td>0.18</td>\n",
       "      <td>0.19</td>\n",
       "      <td>0.90</td>\n",
       "      <td>1.21</td>\n",
       "      <td>2.46</td>\n",
       "      <td>0.00</td>\n",
       "      <td>3.02</td>\n",
       "      <td>&lt;0.005</td>\n",
       "      <td>8.63</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Capitaldeposit</th>\n",
       "      <td>-1.30</td>\n",
       "      <td>0.27</td>\n",
       "      <td>0.14</td>\n",
       "      <td>-1.57</td>\n",
       "      <td>-1.04</td>\n",
       "      <td>0.21</td>\n",
       "      <td>0.35</td>\n",
       "      <td>0.00</td>\n",
       "      <td>-9.57</td>\n",
       "      <td>&lt;0.005</td>\n",
       "      <td>69.61</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Obtaininvest</th>\n",
       "      <td>0.05</td>\n",
       "      <td>1.05</td>\n",
       "      <td>0.28</td>\n",
       "      <td>-0.49</td>\n",
       "      <td>0.59</td>\n",
       "      <td>0.61</td>\n",
       "      <td>1.80</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.17</td>\n",
       "      <td>0.86</td>\n",
       "      <td>0.21</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Joinasso</th>\n",
       "      <td>-0.57</td>\n",
       "      <td>0.57</td>\n",
       "      <td>0.23</td>\n",
       "      <td>-1.02</td>\n",
       "      <td>-0.12</td>\n",
       "      <td>0.36</td>\n",
       "      <td>0.89</td>\n",
       "      <td>0.00</td>\n",
       "      <td>-2.48</td>\n",
       "      <td>0.01</td>\n",
       "      <td>6.26</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Autobid</th>\n",
       "      <td>-0.21</td>\n",
       "      <td>0.81</td>\n",
       "      <td>0.09</td>\n",
       "      <td>-0.39</td>\n",
       "      <td>-0.03</td>\n",
       "      <td>0.68</td>\n",
       "      <td>0.97</td>\n",
       "      <td>0.00</td>\n",
       "      <td>-2.35</td>\n",
       "      <td>0.02</td>\n",
       "      <td>5.72</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Transright</th>\n",
       "      <td>-0.54</td>\n",
       "      <td>0.58</td>\n",
       "      <td>0.11</td>\n",
       "      <td>-0.76</td>\n",
       "      <td>-0.33</td>\n",
       "      <td>0.47</td>\n",
       "      <td>0.72</td>\n",
       "      <td>0.00</td>\n",
       "      <td>-5.04</td>\n",
       "      <td>&lt;0.005</td>\n",
       "      <td>21.01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Riskdeposit</th>\n",
       "      <td>-0.12</td>\n",
       "      <td>0.89</td>\n",
       "      <td>0.27</td>\n",
       "      <td>-0.64</td>\n",
       "      <td>0.40</td>\n",
       "      <td>0.53</td>\n",
       "      <td>1.49</td>\n",
       "      <td>0.00</td>\n",
       "      <td>-0.45</td>\n",
       "      <td>0.65</td>\n",
       "      <td>0.62</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Thirdguarantee</th>\n",
       "      <td>-0.20</td>\n",
       "      <td>0.82</td>\n",
       "      <td>0.23</td>\n",
       "      <td>-0.64</td>\n",
       "      <td>0.25</td>\n",
       "      <td>0.53</td>\n",
       "      <td>1.28</td>\n",
       "      <td>0.00</td>\n",
       "      <td>-0.87</td>\n",
       "      <td>0.38</td>\n",
       "      <td>1.39</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table><br><div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Concordance</th>\n",
       "      <td>0.67</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Partial AIC</th>\n",
       "      <td>9319.66</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>log-likelihood ratio test</th>\n",
       "      <td>292.87 on 9 df</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>-log2(p) of ll-ratio test</th>\n",
       "      <td>189.59</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/latex": [
       "\\begin{tabular}{lrrrrrrrrrrr}\n",
       " & coef & exp(coef) & se(coef) & coef lower 95% & coef upper 95% & exp(coef) lower 95% & exp(coef) upper 95% & cmp to & z & p & -log2(p) \\\\\n",
       "covariate &  &  &  &  &  &  &  &  &  &  &  \\\\\n",
       "RegCapital & 0.00 & 1.00 & 0.00 & -0.00 & 0.00 & 1.00 & 1.00 & 0.00 & 0.30 & 0.77 & 0.38 \\\\\n",
       "Background & 0.55 & 1.73 & 0.18 & 0.19 & 0.90 & 1.21 & 2.46 & 0.00 & 3.02 & 0.00 & 8.63 \\\\\n",
       "Capitaldeposit & -1.30 & 0.27 & 0.14 & -1.57 & -1.04 & 0.21 & 0.35 & 0.00 & -9.57 & 0.00 & 69.61 \\\\\n",
       "Obtaininvest & 0.05 & 1.05 & 0.28 & -0.49 & 0.59 & 0.61 & 1.80 & 0.00 & 0.17 & 0.86 & 0.21 \\\\\n",
       "Joinasso & -0.57 & 0.57 & 0.23 & -1.02 & -0.12 & 0.36 & 0.89 & 0.00 & -2.48 & 0.01 & 6.26 \\\\\n",
       "Autobid & -0.21 & 0.81 & 0.09 & -0.39 & -0.03 & 0.68 & 0.97 & 0.00 & -2.35 & 0.02 & 5.72 \\\\\n",
       "Transright & -0.54 & 0.58 & 0.11 & -0.76 & -0.33 & 0.47 & 0.72 & 0.00 & -5.04 & 0.00 & 21.01 \\\\\n",
       "Riskdeposit & -0.12 & 0.89 & 0.27 & -0.64 & 0.40 & 0.53 & 1.49 & 0.00 & -0.45 & 0.65 & 0.62 \\\\\n",
       "Thirdguarantee & -0.20 & 0.82 & 0.23 & -0.64 & 0.25 & 0.53 & 1.28 & 0.00 & -0.87 & 0.38 & 1.39 \\\\\n",
       "\\end{tabular}\n"
      ],
      "text/plain": [
       "<lifelines.CoxPHFitter: fitted with 968 total observations, 194 right-censored observations>\n",
       "             duration col = 'Duration'\n",
       "                event col = 'Collapse'\n",
       "      baseline estimation = breslow\n",
       "   number of observations = 968\n",
       "number of events observed = 774\n",
       "   partial log-likelihood = -4650.83\n",
       "         time fit was run = 2024-12-31 06:31:29 UTC\n",
       "\n",
       "---\n",
       "                coef exp(coef)  se(coef)  coef lower 95%  coef upper 95% exp(coef) lower 95% exp(coef) upper 95%\n",
       "covariate                                                                                                       \n",
       "RegCapital      0.00      1.00      0.00           -0.00            0.00                1.00                1.00\n",
       "Background      0.55      1.73      0.18            0.19            0.90                1.21                2.46\n",
       "Capitaldeposit -1.30      0.27      0.14           -1.57           -1.04                0.21                0.35\n",
       "Obtaininvest    0.05      1.05      0.28           -0.49            0.59                0.61                1.80\n",
       "Joinasso       -0.57      0.57      0.23           -1.02           -0.12                0.36                0.89\n",
       "Autobid        -0.21      0.81      0.09           -0.39           -0.03                0.68                0.97\n",
       "Transright     -0.54      0.58      0.11           -0.76           -0.33                0.47                0.72\n",
       "Riskdeposit    -0.12      0.89      0.27           -0.64            0.40                0.53                1.49\n",
       "Thirdguarantee -0.20      0.82      0.23           -0.64            0.25                0.53                1.28\n",
       "\n",
       "                cmp to     z      p  -log2(p)\n",
       "covariate                                    \n",
       "RegCapital        0.00  0.30   0.77      0.38\n",
       "Background        0.00  3.02 <0.005      8.63\n",
       "Capitaldeposit    0.00 -9.57 <0.005     69.61\n",
       "Obtaininvest      0.00  0.17   0.86      0.21\n",
       "Joinasso          0.00 -2.48   0.01      6.26\n",
       "Autobid           0.00 -2.35   0.02      5.72\n",
       "Transright        0.00 -5.04 <0.005     21.01\n",
       "Riskdeposit       0.00 -0.45   0.65      0.62\n",
       "Thirdguarantee    0.00 -0.87   0.38      1.39\n",
       "---\n",
       "Concordance = 0.67\n",
       "Partial AIC = 9319.66\n",
       "log-likelihood ratio test = 292.87 on 9 df\n",
       "-log2(p) of ll-ratio test = 189.59"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "X3 = reg_data1[['RegCapital','Background','Capitaldeposit','Obtaininvest','Joinasso',\n",
    "                'Autobid','Transright','Riskdeposit','Thirdguarantee']]\n",
    "covariates = X3\n",
    "from lifelines.statistics import logrank_test\n",
    "from lifelines import CoxPHFitter\n",
    "# 创建CoxPHFitter对象并拟合模型\n",
    "cph = CoxPHFitter()\n",
    "cph.fit(reg_data, duration_col='Duration', event_col='Collapse') \n",
    "# 打印模型摘要\n",
    "cph.print_summary()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          feature       VIF\n",
      "0      RegCapital  1.067827\n",
      "1      Background  1.423915\n",
      "2  Capitaldeposit  1.404760\n",
      "3    Obtaininvest  1.122652\n",
      "4        Joinasso  1.254685\n",
      "5         Autobid  1.539639\n",
      "6      Transright  1.410402\n",
      "7     Riskdeposit  1.081873\n",
      "8  Thirdguarantee  1.103995\n"
     ]
    }
   ],
   "source": [
    "vif_data = pd.DataFrame()\n",
    "vif_data['feature'] = X3.columns\n",
    "# 计算VIF\n",
    "vif_data['VIF'] = [variance_inflation_factor(X3.values, i) for i in range(X3.shape[1])]\n",
    "# 打印VIF DataFrame\n",
    "print(vif_data)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
